package com.feisi.controller;

import com.feisi.domain.Customer;
import com.feisi.domain.Linkman;
import com.feisi.service.CustomerService;
import com.feisi.service.LinkmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/linkman")
public class LinkmanController {
    @Autowired
    private LinkmanService linkmanService;
    @Autowired
    private CustomerService customerService;

    /**
     * 查询所有联系人
     *
     * @param request
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(HttpServletRequest request) {
        System.out.println("表现层:查询所有联系人");
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        List<Linkman> linkman = linkmanService.findAll((page - 1) * limit, limit);
        Integer count = linkmanService.linkmansTotal();
        System.out.println(linkman);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", linkman);
        return map;
    }

    /**
     * 根据联系人名称查联系人
     */
    @RequestMapping("/findByName")
    public @ResponseBody
    Map<String, Object> findByName(String li_name, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        List<Linkman> linkmen = linkmanService.findByName(li_name, (page - 1) * limit, limit);
        int count = linkmanService.linkmansTotalByName(li_name);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", linkmen);
        return map;
    }


    /**
     * 根据客户名查联系人
     * @param customername
     * @param page
     * @param limit
     * @return
     */
    public @ResponseBody
    Map<String, Object> findByCustomerName(@RequestParam("cu_name") String customername, @RequestParam("page") Integer page,
                                           @RequestParam("limit") Integer limit) {
        List<Linkman> linkmen = linkmanService.findByCustomerName(customername,page,limit);
        int count = linkmanService.linkmanTotalByCustomerName(customername);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", linkmen);
        return map;
    }


    /**
     * 新增联系人
     *
     * @param li_name
     * @param li_phone
     * @param li_e_mail
     * @param li_job
     * @param li_sex
     * @param li_comment
     * @param li_create_time
     * @param li_creator
     * @param customername
     * @throws ParseException
     */
    @RequestMapping("/saveLinkman")
    public  @ResponseBody
    Map<String, Object> saveLinkman(String li_name, String li_phone, String li_e_mail, String li_job, String li_sex,String li_policymaker, String li_comment,
                            String li_create_time, String li_creator, @RequestParam("cu_name") String customername) throws ParseException {
        Linkman linkman = new Linkman();
        linkman.setLi_name(li_name);
        linkman.setLi_phone(li_phone);
        linkman.setLi_e_mail(li_e_mail);
        linkman.setLi_job(li_job);
        linkman.setLi_sex(li_sex);
        linkman.setLi_policymaker(li_policymaker);
        linkman.setLi_comment(li_comment);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        linkman.setLi_create_time(df.format(new Date()));
        linkman.setLi_creator(li_creator);
        Customer customer = customerService.findByName(customername, 0, 10).get(0);
        Integer li_customer_id = customer.getId();
        linkman.setLi_customer_id(li_customer_id);
        String li_principal = customer.getUser().getU_name();//得到联系人负责人的姓名
        linkman.setLi_principal(li_principal);
        System.out.println(linkman);
        linkmanService.saveLinkman(linkman);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;

    }

    /**
     * 根据id删除联系人
     * 批量删除
     *
     * @param ids
     */
    @RequestMapping(value = "/deleteLinkman",method = {RequestMethod.POST})
    public @ResponseBody
    Map<String, Object> deleteCustomer(@RequestBody String[] ids) {
        Integer[] id = new Integer[ids.length];
        System.out.println(ids.length);
        for (int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
            Integer idOne = Integer.valueOf(ids[i]);
            id[i]=idOne;

        }
        linkmanService.deleteLinkman(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }


    /**
     * 修改联系人
     *
     * @param li_name
     * @param li_phone
     * @param li_e_mail
     * @param li_job
     * @param li_sex
     * @param li_comment
     * @param li_create_time
     * @param li_creator
     * @param customername
     * @throws ParseException
     */
    @RequestMapping("/updateLinkman")
    public  @ResponseBody
    Map<String, Object> updateLinkman(String id, String li_name, String li_phone, String li_e_mail, String li_job, String li_sex, String li_policymaker, String li_comment,
                                      String li_create_time, String li_creator, @RequestParam("cu_name") String customername) throws ParseException {
        System.out.println("id="+id+",li_name="+li_name+",li_phone="+li_phone+
                ",li_e_mail="+li_e_mail+",li_job="+li_job+",li_sex="+li_sex+
                ",li_policymaker="+li_policymaker+",li_comment="+li_comment+",li_create_time="+li_create_time+
                ",li_creator="+li_creator+",customername="+customername);
        Linkman linkman = new Linkman();
        linkman.setId(Integer.valueOf(id));
        linkman.setLi_name(li_name);
        linkman.setLi_phone(li_phone);
        linkman.setLi_e_mail(li_e_mail);
        linkman.setLi_job(li_job);
        linkman.setLi_sex(li_sex);
        linkman.setLi_policymaker(li_policymaker);
        linkman.setLi_comment(li_comment);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        linkman.setLi_create_time(df.format(new Date()));
        linkman.setLi_creator(li_creator);
        Customer customer = customerService.findByName(customername, 0, 10).get(0);
        Integer li_customer_id = customer.getId();
        linkman.setLi_customer_id(li_customer_id);
        String li_principal = customer.getUser().getU_name();//得到联系人负责人的姓名
        linkman.setLi_principal(li_principal);
        System.out.println(linkman);
        linkmanService.updateLinkman(linkman);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;

    }


}
